package com.my.xinyidaixi.Activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.my.xinyidaixi.DataBase.Model.Over_UserModer;
import com.my.xinyidaixi.DataBase.Model.UserModel;
import com.my.xinyidaixi.Globe.MyUrl;
import com.my.xinyidaixi.HttpVolley.Volley_core;
import com.my.xinyidaixi.R;
import com.my.xinyidaixi.Util.ToastUtil;
import com.wevey.selector.dialog.NormalAlertDialog;

/**
 * Created by zw on 2017/6/20.
 */
public class gengduo extends Activity {

    private ImageView gengduo_fanhui;
    private Intent intent;
    private TextView gengduo_baick;
    private UserModel moder=new Over_UserModer();
    private RelativeLayout xidichangshi;
    private RelativeLayout guanyuwomen;
    private RelativeLayout changjianwenti;
    private RelativeLayout fuwurexian;
    private String num = "041166328728";
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gengduo_layout);
        initdata();

    }

    public void initdata()
    {
        gengduo_fanhui = (ImageView) findViewById(R.id.daixi_fanhui);
        gengduo_fanhui.setOnClickListener(new button());
        gengduo_baick = (TextView) findViewById(R.id.gengduo_back);
        gengduo_baick.setOnClickListener(new button());
        xidichangshi = (RelativeLayout) findViewById(R.id.gengduo_xidichangshi);
        xidichangshi.setOnClickListener(new button());
        guanyuwomen = (RelativeLayout) findViewById(R.id.gengduo_guanyuwomen);
        guanyuwomen.setOnClickListener(new button());
        changjianwenti = (RelativeLayout) findViewById(R.id.gengduo_changjianwenti);
        changjianwenti.setOnClickListener(new button());
        fuwurexian = (RelativeLayout) findViewById(R.id.gengduo_fuwurexian);
        fuwurexian.setOnClickListener(new button());
    }



    public class button implements View.OnClickListener
    {

        @Override
        public void onClick(View v) {

            switch (v.getId())
            {
                case R.id.daixi_fanhui:
                   gengduo.this.finish();
                    gengduo.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                    break;
                case R.id.gengduo_back:
                    gengduo_back_Dialog();
                    break;
                case R.id.gengduo_xidichangshi:
                    intent=new Intent();
                    intent.setClass(gengduo.this,ZongHe_WebView.class);
                    intent.putExtra("url", MyUrl.xidichangshi_url);
                    intent.putExtra("text","洗涤常识");
                    startActivity(intent);
                   gengduo.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                    break;
                case R.id.gengduo_guanyuwomen:
                    intent=new Intent();
                    intent.setClass(gengduo.this,ZongHe_WebView.class);
                    intent.putExtra("url", MyUrl.service_introduction_url);
                    intent.putExtra("text","关于我们");
                    startActivity(intent);
                    gengduo.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                    break;
                case R.id.gengduo_changjianwenti:
                    intent=new Intent();
                    intent.setClass(gengduo.this,changjianwenti.class);
                    startActivity(intent);
                    gengduo.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                    break;
                case R.id.gengduo_fuwurexian:
                    fuwurexian_Dialog();
                    break;
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        gengduo.this.overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
    }

    //自定义对话框
    private void gengduo_back_Dialog() {

        new NormalAlertDialog.Builder(gengduo.this).setTitleVisible(false)
                .setContentText("确定退出当前账户？")
                .setContentTextColor(R.color.cyan)
                .setLeftButtonText("确定")
                .setLeftButtonTextColor(R.color.black)
                .setRightButtonText("不确定")
                .setRightButtonTextColor(R.color.black)
                .setOnclickListener(new com.wevey.selector.dialog.DialogInterface.OnLeftAndRightClickListener<NormalAlertDialog>() {
                    @Override
                    public void clickLeftButton(NormalAlertDialog dialog, View view) {

                        //用户退出的时候 并上传最新的地址 好下次登陆的时候获取
                        String num="";
                        num=moder.getUser();
                        if(num!="")
                        {
                            Volley_core volley=new Volley_core();
                            volley.updateadd(moder.getUser(),moder.get_address());
                        }

                        if(moder.deleteAll())
                        {
                            ToastUtil.shortToast(gengduo.this,"删除成功");
                        }
                        dialog.dismiss();
                    }

                    @Override
                    public void clickRightButton(NormalAlertDialog dialog, View view) {

                        dialog.dismiss();
                    }
                })
                .build()
                .show();

    }

    private void fuwurexian_Dialog() {

        new NormalAlertDialog.Builder(gengduo.this).setTitleVisible(false)
                .setContentText("服务热线：0411-66328728")
                .setContentTextColor(R.color.black)
                .setLeftButtonText("确定")
                .setLeftButtonTextColor(R.color.black)
                .setRightButtonText("不确定")
                .setRightButtonTextColor(R.color.black)
                .setOnclickListener(new com.wevey.selector.dialog.DialogInterface.OnLeftAndRightClickListener<NormalAlertDialog>() {
                    @Override
                    public void clickLeftButton(NormalAlertDialog dialog, View view) {

                       /*model.delete_order(id);*/

                        // 检查是否获得了权限（Android6.0运行时权限）
                        if (ContextCompat.checkSelfPermission(gengduo.this,
                                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
                            // 没有获得授权，申请授权
                            if (ActivityCompat.shouldShowRequestPermissionRationale(gengduo.this,
                                    Manifest.permission.CALL_PHONE)) {
                                // 返回值：
//                          如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
//                          如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
//                          如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
                                // 弹窗需要解释为何需要该权限，再次请求授权
                              //  Toast.makeText(gengduo.this, "请授权！", Toast.LENGTH_LONG).show();

                                // 帮跳转到该应用的设置界面，让用户手动授权
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);
                            }else{
                                // 不需要解释为何需要该权限，直接请求授权
                                ActivityCompat.requestPermissions(gengduo.this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
                            }
                        }else
                        {
                            // 已经获得授权，可以打电话
                            CallPhone();
                        }

                        dialog.dismiss();//对话框消失
                    }

                    @Override
                    public void clickRightButton(NormalAlertDialog dialog, View view) {

                        dialog.dismiss();
                    }
                })
                .build()
                .show();

    }

    private void CallPhone() {
        String number =num;

            // 拨号：激活系统的拨号组件
            Intent intent = new Intent(); // 意图对象：动作 + 数据
            intent.setAction(Intent.ACTION_DIAL); // 设置动作
            Uri data = Uri.parse("tel:" + number); // 设置数据
            intent.setData(data);
            startActivity(intent); // 激活Activity组件

    }

    // 处理权限申请的回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 授权成功，继续打电话
                    CallPhone();
                } else {
                    // 授权失败！
                   // Toast.makeText(this, "授权失败！", Toast.LENGTH_LONG).show();
                }
                break;
            }
        }
    }
}
